﻿#include "pipelinesettingwid.h"
#include <QTableWidgetItem>
#include "ui_pipelinesettingwid.h"
#include "cfgjsonrw.h"
#include "pathsettingwid.h"
#include "paramadddialog.h"



PipelineSettingWid::PipelineSettingWid(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::PipelineSettingWid())
{
	ui->setupUi(this);
	setStyleSheet("font: 12pt \"Agency FB\"");
	QVBoxLayout* lay = new QVBoxLayout(ui->groupBox_3);


	m_path = new PathSettingWid();
	lay->addWidget(m_path);
	auto button = new QPushButton("保存", this);
	connect(button, &QPushButton::clicked, this, &PipelineSettingWid::on_save_path);
	lay->addWidget(button);
}

PipelineSettingWid::~PipelineSettingWid()
{
	delete ui;
}

void PipelineSettingWid::AddPathToTabWidget(PathCfg& info)
{
	int i = ui->tableWidget->rowCount();
	ui->tableWidget->insertRow(i);
	m_paths.insert(info.name(), info);
	auto item1 = new QTableWidgetItem(info.name());
	item1->setTextAlignment(Qt::AlignCenter);
	ui->tableWidget->setItem(i, 0, item1);
	// 添加到删除列表
	QStringList na = info.name().split("-");
	if (ui->comboBox_4->findText(na[0]) == -1) {
		ui->comboBox_4->addItem(na[0]);
	}

}

void PipelineSettingWid::on_tableWidget_cellPressed(int row, int colum)
{
	on_save_path();
	auto pathName = ui->tableWidget->item(row, colum)->text();
	if (m_paths.contains(pathName)) {
		m_path->Init(m_paths[pathName]);
	} else {
		PathCfg cfg = {};
		cfg.name = "未找到路径";
		m_path->Init(cfg);
	}

}
void PipelineSettingWid::on_save_path()
{
	auto cfg = m_path->GetCfg();
	if (m_paths.contains(cfg.name())) {
		m_paths[cfg.name()] = cfg;
	}
}

PathCfg PipelineSettingWid::SavePathFromTabWidget(int row)
{
	PathCfg sel = {};
	auto item1 = ui->tableWidget->item(row, 0);
	QString text = item1->text();
	int num = text.indexOf(",");
	int minAngle = text.mid(1, num - 1).toInt();
	int num2 = text.indexOf(")");
	int maxAngle = text.mid(num + 1, num2 - num - 1).toInt();
	if (maxAngle <= minAngle) {
		QMessageBox::warning(this, "ERROR", QString("路径的角度范围获取异常, min = %1, max = %2").arg(minAngle).arg(maxAngle));
		return {};
	}
	auto item2 = ui->tableWidget->item(row, 1);

	return sel;
}

void PipelineSettingWid::Init(const PipelineCfg& cfg)
{
	on_pushButton_clicked();
	ui->lineEdit_2->setText(cfg.id());
	ui->comboBox->setCurrentIndex(cfg.type());
	auto rob = cfgrw->GetRobotCfg();
	ui->lineEdit->setText(rob.id());


	ui->comboBox_2->setCurrentText(cfg.start());
	ui->comboBox_3->setCurrentText(cfg.end());
	auto paths = cfg.paths();

	for (auto& path : paths) {
		AddPathToTabWidget(path);
	}

}
PipelineCfg PipelineSettingWid::GetCfg()
{
	PipelineCfg cfg = {};
	cfg.id = ui->lineEdit_2->text();
	cfg.type() = ui->comboBox->currentIndex();
	cfg.rob() = ui->lineEdit->text();
	cfg.start() = ui->comboBox_2->currentText();
	cfg.end() = ui->comboBox_3->currentText();
	for (auto& iter : m_paths) {
		cfg.paths().append(iter);
	}

	return cfg;
}

void PipelineSettingWid::on_pushButton_clicked()
{
	ui->comboBox_2->clear();
	ui->comboBox_3->clear();
	int type = ui->comboBox->currentIndex();
	RefreshPoint(type);
}

void PipelineSettingWid::on_comboBox_currentIndexChanged(int index)
{
	RefreshPoint(index);
}

void PipelineSettingWid::RefreshPoint(int type)
{
	auto box = cfgrw->GetAllBoxlineID();
	auto stack = cfgrw->GetAllStackPlateID();
	auto unstack = cfgrw->GetAllUnstackPlateID();
	auto platestack = cfgrw->GetPlateStackCfg();
	ui->comboBox_2->clear();
	ui->comboBox_3->clear();
	switch (type) {
	case 0:	// 码垛
		ui->comboBox_2->addItems(box);
		ui->comboBox_3->addItems(stack);
		m_pathNum = 3;
		break;
	case 1: // 拆垛
		ui->comboBox_2->addItems(unstack);
		ui->comboBox_3->addItems(box);
		m_pathNum = 4;
		break;
	case 2: // 先拆再码
		ui->comboBox_2->addItems(unstack);
		ui->comboBox_3->addItems(stack);
		m_pathNum = 4;
		break;
	case 3: // 拆托盘
		ui->comboBox_2->addItem(platestack.id());
		ui->comboBox_3->addItems(stack);
		m_pathNum = 3;
		break;
	case 4: //码托盘
		ui->comboBox_2->addItems(unstack);
		ui->comboBox_3->addItem(platestack.id());
		m_pathNum = 3;
		break;
	default:
		break;
	}
}

// 增加路经
void PipelineSettingWid::on_pushButton_2_clicked()
{
	QStringList list;
	QString path = ui->comboBox_4->currentText();
	ui->comboBox_4->removeItem(ui->comboBox_4->currentIndex());
	for (auto& iter = m_paths.begin(); iter != m_paths.end(); ++iter) {
		if (iter.value().name().contains(path)) {
			list.append(iter.value().name());
		}
	}

	for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
		QString text = ui->tableWidget->item(i, 0)->text();
		for (auto& iter : list) {
			if (text == iter) {
				ui->tableWidget->removeRow(i);
				m_paths.remove(iter);
				i--;
			}
		}
	}

}

// 添加路径
void PipelineSettingWid::on_pushButton_3_clicked()
{
	QString path = ui->lineEdit_3->text();
	if (path.isEmpty()) {
		return;
	}
	int count = ui->comboBox_4->count();
	ui->comboBox_4->addItem(path);

	if (ui->comboBox_4->count() <= count) {
		QMessageBox::warning(this, "ERROR", "路径名称重复");
		return;
	}
	for (int i = 0; i < m_pathNum; i++) {
		PathCfg temp = {};
		temp.id = i;
		temp.name = path + "-" + QString::number(i);
		AddPathToTabWidget(temp);
	}
	ui->lineEdit_3->setText("");
}
// 删除路径
void PipelineSettingWid::on_pushButton_4_clicked()
{
	int row = ui->tableWidget->currentRow();
	if (row < 0 || row >= ui->tableWidget->rowCount()) {
		QMessageBox::warning(this, "ERROR", "未选择删除对象");
		return;
	}
	ui->tableWidget->removeRow(row);
}
